<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="18" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>小程序配置</span>
          </div>
          <el-form ref="dataForm" :rules="rules" :model="dataForm" label-width="200px">
            <el-tabs>
              <el-tab-pane label="首页配置">
                <el-form-item label="新品首发栏目商品显示数量" prop="litemall_wx_index_new">
                  <el-input v-model="dataForm.litemall_wx_index_new" :style="{width: '60%'}"/>
                </el-form-item>
                <el-form-item label="人气推荐栏目商品显示数量" prop="litemall_wx_index_hot">
                  <el-input v-model="dataForm.litemall_wx_index_hot" :style="{width: '60%'}"/>
                </el-form-item>
                <el-form-item label="店铺直供栏目店铺显示数量" prop="litemall_wx_index_brand">
                  <el-input v-model="dataForm.litemall_wx_index_brand" :style="{width: '60%'}"/>
                </el-form-item>
                <el-form-item label="专题精选栏目显示数量" prop="litemall_wx_index_topic">
                  <el-input v-model="dataForm.litemall_wx_index_topic" :style="{width: '60%'}"/>
                </el-form-item>
                <el-form-item label="分类栏目显示数量" prop="litemall_wx_catlog_list">
                  <el-input v-model="dataForm.litemall_wx_catlog_list" :style="{width: '60%'}"/>
                </el-form-item>
                <el-form-item label="分类栏目商品显示数量" prop="litemall_wx_catlog_goods">
                  <el-input v-model="dataForm.litemall_wx_catlog_goods" :style="{width: '60%'}"/>
                </el-form-item>
              </el-tab-pane>
              <el-tab-pane label="其他配置">
                <el-form-item label="商品分享功能" prop="litemall_wx_share">
                  <el-radio-group v-model="dataForm.litemall_wx_share">
                    <el-radio :label="true+''">开启</el-radio>
                    <el-radio :label="false+''">关闭</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="微信支付功能" prop="litemall_wx_pay">
                  <el-radio-group v-model="dataForm.litemall_wx_pay">
                    <el-radio :label="true+''">开启</el-radio>
                    <el-radio :label="false+''">关闭</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-tab-pane>
            </el-tabs>
            <el-form-item>
              <el-button @click="cancel">取消</el-button>
              <el-button type="primary" @click="update">确定</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listWx, updateWx } from '@/api/config'

export default {
  name: 'ConfigWx',
  data() {
    return {
      dataForm: {
        litemall_wx_index_new: 0,
        litemall_wx_index_hot: 0,
        litemall_wx_index_brand: 0,
        litemall_wx_index_topic: 0,
        litemall_wx_catlog_list: 0,
        litemall_wx_catlog_goods: 0,
        litemall_wx_share: false,
        litemall_wx_pay: true
      },
      rules: {
        litemall_wx_index_new: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        litemall_wx_index_hot: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        litemall_wx_index_brand: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        litemall_wx_index_topic: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        litemall_wx_catlog_list: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        litemall_wx_catlog_goods: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init: function() {
      listWx().then(response => {
        this.dataForm = response.data.data;
      })
    },
    cancel() {
      this.init()
    },
    update() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.doUpdate()
      })
    },
    doUpdate() {
      updateWx(this.dataForm)
        .then(response => {
          this.$notify.success({
            title: '成功',
            message: '小程序配置成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    }
  }
}
</script>
